package com.zfy.cmsservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zfy.cmsservice.entity.CourseSelections;
import com.zfy.cmsservice.entity.Subjects;
import com.zfy.cmsservice.entity.Teachers;
import com.zfy.cmsservice.service.CourseSelectionsService;
import com.zfy.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 选课表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-12-17
 */
@RestController
@RequestMapping("/cmsservice/course-selections")
@Api(description = "选课管理-选课信息维护")
public class CourseSelectionsController {
    @Autowired
    private CourseSelectionsService courseSelectionsService;

    // 添加选课
    @ApiOperation(value = "添加选课")
    @PostMapping("addCourseSelection")
    public R addCourseSelection(@RequestBody CourseSelections courseSelections){
        QueryWrapper<CourseSelections> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id",courseSelections.getStudentId());
        wrapper.eq("is_deleted",0);
        CourseSelections find = courseSelectionsService.getOne(wrapper);
        //  如果有记录不进行添加
        if(find == null){
            boolean save = courseSelectionsService.save(courseSelections);
            if(save) {
                return R.ok().message("添加成功");
            }
            return R.error().message("添加失败");
        }else {
            return R.error().message("已经有选课记录，请联系管理员");
        }
    }

//    修改选课
    @ApiOperation(value = "修改选课信息")
    @PostMapping("updateCourseSelection")
    public R updateCourseSelection(@RequestBody CourseSelections courseSelections){
        if (StringUtils.isEmpty(courseSelections)) {
            // 传参错误异常
            return R.httpError();
        }
        boolean update = courseSelectionsService.updateById(courseSelections);
        if(update) {
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

//    逻辑删除选课信息
    @ApiOperation(value = "删除选课")
    @PostMapping("removeCourseSelection/{id}")
    public R removeCourseSelection(@PathVariable String id) {
        boolean remove = courseSelectionsService.removeById(id);
        if(remove) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    //根据id查询教师
    @ApiOperation(value = "根据id查询教师")
    @GetMapping("getCourseSelection/{id}")
    public R getTeacherById(@PathVariable String id) {
        CourseSelections courseSelections = courseSelectionsService.getById(id);
        return R.ok().data("item", courseSelections);
    }

    // 分页查询选课信息
    @ApiOperation(value = "分页查询选课信息")
    @GetMapping("pageCourseSelection/{page}/{size}")
    public R pageCourseSelection(@PathVariable long page, @PathVariable long size,
                                 @RequestParam(required = false) String courseId) {
        return courseSelectionsService.pageCourseSelection(page,size,courseId);
    }
}

